//
//  FenleiViewController.m
//  tianjin
//
//  Created by 鞠超 on 16/8/5.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "FenleiViewController.h"

#import "SousuoViewController.h"
#import "Right_BtnViewController.h"
#import "FenleiMingchengTableViewCell.h"
#import "FenleiProductCollectionViewCell.h"
#import "FenleiHeadNameCollectionReusableView.h"
#import "ChanpinliebiaoViewController.h"

@interface FenleiViewController () <UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) UICollectionView * collectionView;

//记录一下上一次所在位置
@property (nonatomic, assign) NSInteger old_index;

@end

@implementation FenleiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _lblTitle.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    _topView.backgroundColor = [UIColor blackColor];
    
    //搜索
    UIView * view_topSearch = [[UIView alloc] initWithFrame:CGRectMake(10, 20 + 4.5, SCREEN_WIDTH - 10 - 50, 33.5)];
    view_topSearch.backgroundColor = [UIColor groupTableViewBackgroundColor];
    view_topSearch.layer.cornerRadius = 3;
    [_topView addSubview:view_topSearch];
    
    UIButton * btn_topSearch = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_topSearch.frame = CGRectMake(0, 0, SCREEN_WIDTH - 60, 35);
    [view_topSearch addSubview:btn_topSearch];
    [btn_topSearch addTarget:self action:@selector(btn_topSearchAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIImageView * image_search = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20 + 12, 20, 20)];
    image_search.image = [UIImage imageNamed:@"sousuo@2x.png"];
    [_topView addSubview:image_search];
    
    UILabel * label_search = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_search.frame) + 5, 20 + 14.5, SCREEN_WIDTH - CGRectGetMaxX(image_search.frame) - 5 - 50, 15)];
    label_search.text = @"特价水果限量抢购!超值实惠不容错过";
    label_search.textColor = [UIColor grayColor];
    label_search.font = [UIFont systemFontOfSize:13];
    [_topView addSubview:label_search];
    
    //右侧栏
    UIImageView * image_erji = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 40 + 2.5, 20 + 9.5, 25, 25)];
    image_erji.userInteractionEnabled = YES;
    image_erji.image = [UIImage imageNamed:@"kefu@2x.png"];
    [_topView addSubview:image_erji];
    
    UIButton * btn_image_erji = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_image_erji.frame = CGRectMake(0, 0, 30, 30);
    [image_erji addSubview:btn_image_erji];
    [btn_image_erji addTarget:self action:@selector(btn_image_erjiAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//搜索栏
- (void )btn_topSearchAction:(UIButton *)sender
{
    //    NSLog(@"跳转搜索页面");
    
    SousuoViewController * sousuoViewController = [[SousuoViewController alloc] init];
    [self showViewController:sousuoViewController sender:nil];
}

//右边栏
- (void )btn_image_erjiAction:(UIButton *)sender
{
    //    NSLog(@"右边栏");
    
//    Right_BtnViewController * right_BtnViewController = [[Right_BtnViewController alloc] init];
//    [self showViewController:right_BtnViewController sender:nil];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] showTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    CGFloat length_x = SCREEN_WIDTH / 4;
    //tableview
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, length_x, SCREEN_HEIGHT - 49 - 64) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[FenleiMingchengTableViewCell class] forCellReuseIdentifier:@"cell_feileimingcheng"];
    
    //collection
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    //每个item的大小
    int  item_length = (length_x * 3 - 40) / 3;
    layout.itemSize = CGSizeMake(item_length, item_length + 30);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 5, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(length_x, 64, length_x * 3, SCREEN_HEIGHT - 49 - 64) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.collectionView];
    //注册
    [self.collectionView registerClass:[FenleiProductCollectionViewCell class] forCellWithReuseIdentifier:@"cell_feileiProduct"];
    //头视图注册
    [self.collectionView registerClass:[FenleiHeadNameCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cell_HeaderView"];
}

#pragma mark - collection代理
//有几个分区
- (NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}
//每个分区有多少个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FenleiProductCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell_feileiProduct" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    [cell.image_product sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"fenleiProduct@2x.png"]];
    
    return cell;
}

- (void )collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld,%ld",indexPath.section,(long)indexPath.item);
    
    ChanpinliebiaoViewController * chanpinliebiaoViewController = [[ChanpinliebiaoViewController alloc] init];
    [self showViewController:chanpinliebiaoViewController sender:nil];
}

//设置头尾部内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    //定制头部视图的内容
    FenleiHeadNameCollectionReusableView *headerV = (FenleiHeadNameCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cell_HeaderView" forIndexPath:indexPath];
    reusableView = headerV;
    
    return reusableView;
}
//设置头尾的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 25);
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FenleiMingchengTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_feileimingcheng" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.view_line.hidden = YES;
    cell.name.textColor = navi_bar_bg_color;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.tag = indexPath.row + 100;
    
    if(self.old_index == indexPath.row)
    {
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        cell.view_line.hidden = NO;
        cell.name.textColor = [UIColor blackColor];
    }
    //赋值
    cell.name.text = [NSString stringWithFormat:@"水果-%d",indexPath.row + 1];
    
    return cell;
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FenleiMingchengTableViewCell * old_cell = [self.view viewWithTag:self.old_index + 100];
    old_cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
    old_cell.view_line.hidden = YES;
    old_cell.name.textColor = [UIColor blackColor];
    
    //保存点击记录
    self.old_index = indexPath.row;
    
    FenleiMingchengTableViewCell * new_cell = [self.view viewWithTag:indexPath.row + 100];
    new_cell.backgroundColor = [UIColor whiteColor];
    new_cell.view_line.hidden = NO;
    new_cell.name.textColor = navi_bar_bg_color;
    
    //在这里掉接口,得到小分类的数据
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
        
        [self.collectionView reloadData];
    });
}
















@end
